import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import ElementPlus from 'element-plus'
import { ElMessage } from 'element-plus'
import 'element-plus/dist/index.css'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import { createPinia } from 'pinia'

// 配置ElementPlus消息
ElMessage.closeAll() // 关闭所有消息
// 自定义消息配置，限制同时只显示一条消息
ElMessage._instance = null
const originalMessage = ElMessage
const message = (options) => {
  ElMessage.closeAll()
  return originalMessage(options)
}
['success', 'warning', 'info', 'error'].forEach(type => {
  message[type] = (options) => {
    ElMessage.closeAll()
    return originalMessage[type](options)
  }
})
ElMessage.closeAll = originalMessage.closeAll

// 处理ResizeObserver警告
const originalError = console.error
console.error = (...args) => {
  // 忽略ResizeObserver错误
  if (args[0] && args[0].toString().includes('ResizeObserver')) {
    return
  }
  originalError.apply(console, args)
}

const app = createApp(App)

// 创建pinia实例
const pinia = createPinia()

app.use(router)
app.use(ElementPlus, {
  locale: zhCn
})
app.use(pinia)

// 全局异常处理
app.config.errorHandler = (err, vm, info) => {
  // 忽略ResizeObserver错误
  if (err && err.toString().includes('ResizeObserver')) {
    return false
  }
  console.error('Vue全局异常:', err, info)
  // 避免显示UI错误
  return false
}

app.mount('#app')
